package com.zx.web.servelt;

import com.zx.bean.Cart;
import com.zx.bean.Goods;
import com.zx.bean.User;
import com.zx.web.service.CartService;
import com.zx.web.service.GoodsService;
import com.zx.web.service.impl.CartServiceImpl;
import com.zx.web.service.impl.GoodsServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 章鑫
 * @version V1.0
 * @Project 2022-09-05
 * @Package com.zx.web.servelt
 * @Description:
 * @Date 2022/9/14 16:35
 */
@WebServlet("/cartservlet")
public class CartServlet extends BaseServlet{
    private CartService service = new CartServiceImpl();
    private GoodsService goodsService = new GoodsServiceImpl();
    /**
     * 添加商品到购物车
     *    同一id
     *      如果商品存在  update
     *      商品不存在    add
     * @return
     */
    public String addCart(HttpServletRequest req, HttpServletResponse resp){
        //获取加入到购物车中的商品的数量number和goodsid
        int number = Integer.parseInt(req.getParameter("number"));
        int goodsid = Integer.parseInt(req.getParameter("goodsId"));
        //获取用户的id
        User user = (User) req.getSession().getAttribute("user");
        int userId = user.getId();

        //查询单价
        Goods goodsDetail = goodsService.findGoodsDetail(goodsid);

        //查询，根据userId和goodsid,看是否存在
        Cart cart = service.findCart(userId,goodsid);

        //查到了  执行update
        if(cart != null){
            cart.setNum(cart.getNum()+number);

            cart.setMoney(goodsDetail.getPrice()*cart.getNum());
            //执行更新
            service.updateCart(cart);
        }else{
            cart = new Cart();
            cart.setNum(number);
            cart.setPid(goodsid);
            cart.setId(userId);
            cart.setMoney(goodsDetail.getPrice());
            //执行add
            service.add(cart);
        }

        return "redirect:/cartSuccess.jsp";
    }

    /**
     * 查看购物车
     * @param req
     * @param resp
     * @return
     */
    public String getCart(HttpServletRequest req,HttpServletResponse resp){

        User user = (User) req.getSession().getAttribute("user");


        if(user!=null){
            int userId = user.getId();
            //通过userId查询购物车中商品的pid
            List<Cart> cartList = service.findCartByUserId(userId);
            req.setAttribute("carts",cartList);
            return "forward:/cart.jsp";
        }else{
            return "redirect:/login.jsp";
        }

    }

    /**
     *
     * @param req
     * @param resp
     */
    public void addCartAjax(HttpServletRequest req,HttpServletResponse resp){
        //接收参数
        int goodsId = Integer.parseInt(req.getParameter("goodsId"));
        int number = Integer.parseInt(req.getParameter("number"));
        //获取登录者userId
        User user = (User) req.getSession().getAttribute("user");
        int userId = user.getId();

        if(number==0){
            service.deleteCart(userId,goodsId);
        }else{
            //查询物品详情
            GoodsService serviceG = new GoodsServiceImpl();
            Goods goodsDetail = serviceG.findGoodsDetail(goodsId);
            //查询数据库，看商品是否存在
            Cart cart = service.findCart(userId, goodsId);
            if(cart!=null){
                cart.setNum(cart.getNum()+number);
                cart.setMoney(goodsDetail.getPrice()*cart.getNum());
                service.updateCart(cart);
            }else{
                cart.setId(userId);
                cart.setPid(goodsId);
                cart.setNum(number);
                cart.setMoney(goodsDetail.getPrice());
                service.add(cart);
            }
        }
    }

    /**
     * 清空购物车
     * @param req
     * @param resp
     */
    public void clearCartAjax (HttpServletRequest req,HttpServletResponse resp){
        //获取登录者userId
        User user = (User) req.getSession().getAttribute("user");
        int userId = user.getId();

        service.clearCartById(userId);
    }
}
